#include "monster.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdbool.h>

monster* CreateMonster(const char *name,int hp, int attack,int def,char* symbol,int exp,int golden)
{
    monster* m =(monster*) malloc(sizeof(monster));
    if(m == NULL)
        return NULL;
    m->hp = hp;
    m->symbol = GetString(symbol);
    m->attack = attack;
    m->def = def;
    m->name = GetString(name);
    m->x = 0;
    m->y = 0;
    m->exp = exp;
    m->golden = golden;
    return m;
}

monster* CreateMonsterLoad(int x,int y,const char *name,int hp, int attack,int def,char* symbol,int exp,int golden)
{
    monster* m =(monster*) malloc(sizeof(monster));
    if(m == NULL)
        return NULL;
    m->hp = hp;
    m->symbol = GetString(symbol);
    m->attack = attack;
    m->def = def;
    m->name = GetString(name);
    m->x = x;
    m->y = y;
    m->exp = exp;
    m->golden = golden;
    return m;
}

void SetRandPoint(void *ptr)
{
    monster *m = (monster*)ptr;
    m->x = rand() % 10;
    m->y = rand() % 10;
    while(m->x == 0 && m->y == 0)
    {
        m->x = rand() % 10;
        m->y = rand() % 10;
    }
}

void FreeMonster(void *ptr)
{
    monster *m = (monster*)ptr;
    ClearString(&m->name);
    free(m);
}

void SetMonsters(DArray *ms)
{
    //设置随机种子
    srand(time(NULL));
    for(int i = 0; i < 10; i++)
    {
        ms->InsertArrayTail(ms,CreateMonster("史莱姆",10,3,1," 🥶 ",10,5));
    }
    for(int i = 0; i < 8; i++)
    {
        ms->InsertArrayTail(ms,CreateMonster("哥布林",20,5,3," 👽 ",15,10));
    }
    for(int i = 0; i < 5; i++)
    {
        ms->InsertArrayTail(ms,CreateMonster("骷髅兵",30,10,10," 💀 ",20,20));
    }
    ms->InsertArrayTail(ms,CreateMonster("大黑龙",200,20,20," 🐲 ",0,0));

    ms->DisplayDynamicArray(ms,SetRandPoint);
}

static char* (*MyMap)[MAPSIZE] = NULL;

void SetPosition(void *ptr)
{
    monster *m = (monster* )ptr;
    MyMap[m->x][m->y] = m->symbol.string;
}

bool SortByPoint(void *ptr1, void *ptr2)
{
    monster *m1 = (monster*) ptr1;
    monster *m2 = (monster*) ptr2;
    if(m1->x > m2->x)
    {
        return true;
    }
    else if(m1->x == m2->y)
    {
        return m1->y > m2->y;
    }
    else
    {
        return false;
    }
}

//去重
void SetPosduplicate(DArray *ms)
{
    int flag = 0;
    ms->Sort(ms,SortByPoint);
    for(int i = 1; i < ms->len-1; i++)
    {
        monster *m1 = (monster*)ms->dp[i];
        monster *m2 = (monster*)ms->dp[i+1];
        if(m1->x == m2->x && m1->y == m2->y)
        {
            m2->y++;
            if(m2->y == MAPSIZE)
            {
                flag = 1;
                m2->x++;
                m2->x = m2->x % MAPSIZE;
                m2->y = 1;
            }
        }
    }
    if(flag == 1)
    {
        SetPosduplicate(ms);
    }
}

void PrintMonster(void *ptr)
{
    monster *m = (monster*)ptr;
    printf("name: %s x:%d y:%d\n",m->name.string,m->x,m->y);
}

void SetMonsterPosition(DArray *ms, char* (*map)[MAPSIZE])
{
    //坐标去重
    SetPosduplicate(ms);

    //在地图上显示怪兽
    MyMap = map;
    ms->DisplayDynamicArray(ms,SetPosition);
    MyMap = NULL;
    //ms->DisplayDynamicArray(ms,PrintMonster);

}

monster *GetMonsterByPosition(DArray *ms, int x, int y)
{
    for(int i = 0; i < ms->len; i++)
    {
        monster *m = (monster*)ms->dp[i];
        if(m->x == x && m->y == y && m->hp > 0)
        {
            return m;
        }
    }
    return NULL;
}

MyStr MonstersToString(DArray *ms)
{
    char str[1024] = {0}; 
    char str1[4096] = {0};
    for(int i = 0; i < ms->len; i++)
    {
        monster *m = (monster*)ms->dp[i];
        if(m->hp != 0)
        {
            sprintf(str,"%s %s %d %d %d %d %d %d %d\n",m->symbol.string,m->name.string,m->x,m->y,m->hp,m->attack,m->def,m->exp,m->golden);
            strcat(str1,str);
        }
    }
    strcat(str1,",");
    return GetString(str1);
}